import React, {Component} from 'react';
import {base, toast} from 'nc-lightapp-front';
import proFetch from '../../../../public/functions/project-fetch';

const {NCModal, NCButton, NCCheckbox, NCForm, NCFormControl} = base;
const {Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter} = NCModal;
const {NCFormItem} = NCForm;
import setQueryMeta from "../../container/functions/setQueryMeta";
import HRGroup from "../../../../refer/hiref/KeyPsnGrpGridRef";
import ReferOptions from "../../../../../hrjf/refer/jfref/MSPsnJobRefMo";
import './index.less';

class ManageScopeSet extends Component {
    constructor(props) {
        super(props);
        this.confirmModal = this.confirmModal.bind(this);
        this.closeModal = this.closeModal.bind(this);
        this.onChangeHandle = this.onChangeHandle.bind(this);
        this.hrValueChange = this.hrValueChange.bind(this);
        this.deleteData = this.deleteData.bind(this);
        this.clearData = this.clearData.bind(this);
        this.conditionOptions = this.conditionOptions.bind(this);
        this.openNcModal = this.openNcModal.bind(this);
        this.clickHandle = this.clickHandle.bind(this);
        this.hrGroupChange = this.hrGroupChange.bind(this);
        this.referChange = this.referChange.bind(this);

        this.state = {
            hrChecked: true,//人事管理checkbox
            contractChecked: true,//合同管理checkbox
            salaryChecked: true,//薪酬管理checkbox
            hrGroup: '',//人员组
            json: {},
            referValue: ''//参照选择
        }
    }

    // 获取父级多语
    getLanguage(json) {
        this.setState({
            json: json
        })
    }

    openNcModal() {//开始显示时的钩子函数
        this.props.form.setFormStatus('card3', 'add');
        this.clearData()

    }

    clickHandle(props, moduleId, record, index) {
        this.props.editTable.selectTableRows('card3', index, true)

    }

    confirmModal() {
        let searchResultData = this.props.editTable.getAllRows('list1');
        if (searchResultData.length) {
            this.sureAddFun()
        } else {
            toast({
                color: 'warning',
                content: this.state.json['hi6007-000270'] //请选择人员
            });
        }

    }

    sureAddFun() {
        let searchResultData = this.props.editTable.getAllRows('list1');
        let managescopeBusiRegions = [], pk_psndoc = [], pk_psnjobs = [];
        searchResultData && searchResultData.forEach((v, k) => {
            pk_psndoc.push(v.values.pk_psndoc.value)
            pk_psnjobs.push(v.values.pk_psnjob.value)
        })
        if (this.state.hrChecked) {
            managescopeBusiRegions.push('PSNDOC00000000000000')
        }
        if (this.state.contractChecked) {
            managescopeBusiRegions.push('PSNPACT0000000000000')
        }
        if (this.state.salaryChecked) {
            managescopeBusiRegions.push('SALARY00000000000000')
        }
        let postData = {
            manageScopeType: 'manage_scope_type_psndoc',
            managescopeBusiRegions: managescopeBusiRegions,//管理业务范围
            pk_keypsn_group: this.state.hrGroup && this.state.hrGroup.refpk,//关键人员组主键
            pk_org_dept: pk_psndoc,
            pk_psnjobs: pk_psnjobs,
            pk_orgs: this.props.initData.refpk,//人力资源组织主键
            selectManageScope: 'psn',//组织org、部门dept、人员psn
            funcType: 2//人员
        };
        proFetch({
            url: '/nccloud/hrjf/managescope/AddManageScopeAction.do',
            body: postData,
        }).then((res) => {
            if (res.success) {
                this.closeModal();
                this.props.updateMainTableData(res.data.managescopesettingslist, 1, 'add')
            }
        });
    }

    closeModal() {
        this.props.onClose && this.props.onClose();
    }

    deleteData() {//删除某一行
        let selectData = this.props.editTable.getCheckedRows('list1') || [];
        if (selectData.length) {
            let deleteDataIndex = [];
            selectData.forEach((v, k) => {
                deleteDataIndex.push(v.index)
            })
            this.props.editTable.deleteTableRowsByIndex('list1', deleteDataIndex, true);
        } else {
            toast({
                color: 'warning',
                content: this.state.json['hi6007-000271']//请选择要删除的人员！
            });
        }


    }

    clearData() {//清空表格
        // this.hrGroupChange()
        this.props.editTable.setTableData('list1', {rows: []})
    }

    conditionOptions() {//条件选择
        setQueryMeta.call(this);
        this.props.search.openAdvSearch('managescopequery', true);//条件选择编码

    }

    onChangeHandle(type) {
        return (value) => {
            this.setState({
                [type]: value
            }, () => {

            });
        }
    }

    hrValueChange() {
    }

    hrGroupChange(value) {
        this.setState({
            hrGroup: value
        })
    }

    referChange(value) {
        let historyData = [];
        let searchResultData = this.props.editTable.getAllRows('list1');
        let new_pks = [], old_pks = [];
        value && value.forEach((v, k) => {
            new_pks.push(v.refpk)
        })
        searchResultData.forEach((v, k) => {
            old_pks.push(v.values.pk_psnjob.value)
        })
        let postData = {
            new_pks: new_pks,
            old_pks: old_pks,
            selectManageScope: 'psn'
        };
        proFetch({
            url: '/nccloud/hrjf/managescope/ManageScopeGetVoFromRefer.do',
            body: postData,
        }).then((res) => {
            if (res.success) {
                if (res.data.list1) {
                    historyData = res.data.list1;
                    let newData = JSON.parse(JSON.stringify(historyData))
                    newData.rows = searchResultData.concat(historyData.rows)
                    this.props.editTable.setTableData('list1', newData)
                }


            }
        });

    }

    render() {
        const {
            editTable,
            visible,
            initData
        } = this.props;

        const {
            hrChecked,
            contractChecked,
            salaryChecked,
            hrGroup,
            referValue
        } = this.state;


        return (
            <NCModal
                show={visible}
                size="xlg"
                className="manage-scope-set"
                onHide={this.closeModal}
                backdrop={'static'}
                onEnter={this.openNcModal}
            >
                <NCModalHeader closeButton={true}>
                    {/* 员工管理范围设置 */}
                    {this.state.json['hi6007-000272']}
                </NCModalHeader>
                <NCModalBody>
                    {/* 人力资源组织 */}
                    <NCFormItem labelName={this.state.json['hi6007-000273']} method="blur">
                        <NCFormControl className="demo-input" disabled="disabled"
                                       value={initData.refname}
                                       onChange={this.hrValueChange}
                        />
                    </NCFormItem>
                    <div className="checkbox-content">
                        {/* 管理业务范围 */}
                        <span className="checkbox-title">{this.state.json['hi6007-000258']}</span>
                        <NCCheckbox
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('hrChecked')}
                            checked={hrChecked}
                        >
                            {/* 人事管理 */}
                            {this.state.json['hi6007-000259']}
                        </NCCheckbox>
                        <NCCheckbox
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('contractChecked')}
                            checked={contractChecked}
                        >
                            {/* 合同管理 */}
                            {this.state.json['hi6007-000260']}
                        </NCCheckbox>
                        <NCCheckbox
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('salaryChecked')}
                            checked={salaryChecked}
                        >
                            {/* 薪酬管理 */}
                            {this.state.json['hi6007-000261']}
                        </NCCheckbox>
                    </div>
                    <div className="hr-group-wrapper">
                        {/* 加入关键人员组 */}
                        <label className="hrGroup-title">{this.state.json['hi6007-000274']}</label>
                        <HRGroup className="hr-group"
                                 value={hrGroup}
                                 onChange={this.hrGroupChange}
                                 queryCondition={function () {
                                     return {
                                         pk_org: initData.refpk
                                     }
                                 }}
                        />
                    </div>
                    <div className="operation-button-wrapper">
                        <div className="operation-button">
                        <span className="refer-wrapper"
                        >
                            <ReferOptions
                                className="refer-options"
                                value={referValue}
                                onChange={this.referChange}
                                clickContainer={<NCButton shape="border"
                                >
                                    {/* 参照选择 */}
                                    {this.state.json['hi6007-000264']}
                                </NCButton>}
                            />
                            
                        </span>
                            <span className="button-options">
                            <NCButton shape="border"
                                      onClick={this.conditionOptions}>
                                {/* 条件选择 */}
                                {this.state.json['hi6007-000275']}
                            </NCButton>
                                <NCButton shape="border"
                                          onClick={this.deleteData}>
                                    {/* 删除 */}
                                    {this.state.json['hi6007-000031']}
                            </NCButton>
                                <NCButton
                                    shape="border"
                                    onClick={this.clearData}
                                >
                                    {/* 清空 */}
                                    {this.state.json['hi6007-000084']}
                            </NCButton>
                        </span>
                        </div>
                    </div>
                    {
                        editTable.createEditTable('list1', {
                            onRowClick: this.clickHandle,
                            showCheck: true,
                            showIndex: true,
                            showPagination: true, // 是否展示分页

                        })
                    }

                </NCModalBody>
                <NCModalFooter>
                    <NCButton colors="primary"
                              onClick={this.confirmModal}>
                        {/* 确定 */}
                        {this.state.json['hi6007-000004']}
                    </NCButton>
                    <NCButton
                        shape="border"
                        onClick={this.closeModal}
                    >
                        {/* 取消 */}
                        {this.state.json['hi6007-000005']}
                    </NCButton>
                </NCModalFooter>
            </NCModal>
        );
    }
}

export default ManageScopeSet;
